package com.dashan.backend.datastore;

import java.util.List;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.Query;

public class FriendInvitationDao extends DaoBase {

    @SuppressWarnings("unchecked")
    public List<DsFriendInvitation> getFriendInvitationFrom(String fromUserName) {
        Query query = pm.newQuery(DsFriendInvitation.class);
        query.setFilter("invitationFromUserName == userNameParam");
        query.setOrdering("timestamp asc");
        query.declareParameters("String userNameParam");
        
        List<DsFriendInvitation> result = (List<DsFriendInvitation>) query.execute(fromUserName);
        
        return result;
    }

    @SuppressWarnings("unchecked")
    public List<DsFriendInvitation> getFriendInvitationTo(String toUserName) {
        Query query = pm.newQuery(DsFriendInvitation.class);
        query.setFilter("invitationToUserName == userNameParam");
        query.setOrdering("timestamp asc");
        query.declareParameters("String userNameParam");
        
        List<DsFriendInvitation> result = (List<DsFriendInvitation>) query.execute(toUserName);
        
        return result;
    }
    
    public void addFriendInvitation(DsFriendInvitation request) {
        pm.makePersistent(request);
        pm.flush();
    }
    
    public void removeFriendInvitation(String from, String to) 
    throws JDOObjectNotFoundException {
    
        DsFriendInvitation invitation = 
            pm.getObjectById(DsFriendInvitation.class, DsFriendInvitation.buildKey(from, to));
       
        pm.deletePersistent(invitation);
    }
}
